(() => {

    let ele = {}
    ele.confirm = $('#confirm')//实名认证

    ele.payee = $('#payee')//姓名
    ele.alipay = $('#alipay')//身份证


    init()
    function init() {
        getUserInfo(res => {
            if (res.is_check != 0) {
                ele.confirm.innerHTML = '<span class="border">' + '已绑定' + '</span>'
                ele.confirm.disabled = 'disabled';
                ele.payee.value = res.true_name;
                ele.alipay.value = res.id_card;
                ele.payee.disabled = 'disabled';
                ele.alipay.disabled = 'disabled';
            } else {
                // 点击进行实名认证
                ele.confirm.onclick = () => {
                    let payee = ele.payee.value;
                    let alipay = ele.alipay.value;
                    if (!payee) openTips('姓名不能为空');
                    // else if (!/^[\u4e00-\u9fa5]{2,4}$/.test(payee)) openTips('收款人姓名不合法');
                    else if (payee.length < 2 || payee.length > 12) openTips('姓名不合法');
                    else if (!alipay) openTips('身份证号不能为空');
                    // else if (!/(^1[3-9]\d{9}$)|(^[\w\d-]+(\.[\w\d-]+)*@[\w\d-]+(\.[\w\d-]+)+$)/.test(alipay))
                    // 	openTips('支付宝账号不合法');
                    else face(payee, alipay);
                }
            }

        })
    }


    // 实名认证
    function face(payee, alipay) {
        //请求绑定支付宝接口
        let token = getCookie("token");
        let userid = getCookie("userid");
        ajax({
            url: API_PATH.BINDIDCRAD,
            method: 'POST',
            header: {
                Authorization: token
            },
            data: {
                true_name: payee,// 姓名
                id_card: alipay//身份证号
            },
            success: res => {
                if (res.code == 1) {
                    if (res.data.need_alipay_auth == 1) {
                        let token = getCookie('token')
                        let alipayUrl = `https://openauth.alipay.com/oauth2/publicAppAuthorize.htm?app_id=2021004104609332&scope=auth_user&redirect_uri=http://xwrc.cpnet.cc/api/get_ali_user&state=${token}`
                        let openURL = 'alipays://platformapi/startapp?appId=20000067&url=' + encodeURIComponent(alipayUrl);
                        location.href = openURL
                        init();
                    } else {
                        openTips(res.msg)
                    }

                } else {
                    openTips(res.msg)
                }
            }
        });
    }

})();
